package wuziqi;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//游戏主框架
public class GameFrame extends JFrame implements ActionListener {
    private  GamePanle gamePanle;
    //构造函数，和类名相同，在类对象创建的时候执行
    public GameFrame(){
        Font font=new Font("微软雅黑",Font.PLAIN,12);

        JMenuBar menuBar=new JMenuBar();
        JMenu menu1=new JMenu("游戏");
        menu1.setFont(font);

        JMenuItem menu1_item1=menu1.add("新游戏");
        menu1_item1.setFont(font);

        JMenuItem menu1_item2=menu1.add("悔棋");
        menu1_item2.setFont(font);

        JMenuItem menu1_item3=menu1.add("认输");
        menu1_item3.setFont(font);

        menu1.addSeparator();

        JMenuItem menu1_item4=menu1.add("退出");
        menu1_item4.setFont(font);

        JMenu menu2=new JMenu("帮助");
        menu2.setFont(font);
        JMenuItem menu2_item1=menu2.add("关于");
        menu2_item1.setFont(font);

        menuBar.add(menu1);
        menuBar.add(menu2);

        this.setJMenuBar(menuBar);


        menu1_item1.setActionCommand("new");
        menu1_item1.addActionListener(this);

        menu1_item2.setActionCommand("undo");
        menu1_item2.addActionListener(this);

        menu1_item3.setActionCommand("surrender");
        menu1_item3.addActionListener(this);

        menu1_item4.setActionCommand("exit");
        menu1_item4.addActionListener(this);

        menu2_item1.setActionCommand("about");
        menu2_item1.addActionListener(this);

        //关于窗口的特点
        this.setTitle("五子棋");
        this.setLayout(null);
        this.setSize(Const.窗口宽度,Const.窗口高度);
        this.setLocationRelativeTo(null);  //让窗口居中
        //this.setResizable(false);  //让窗口不能调整大小


        this.gamePanle=new GamePanle();
        this.add(gamePanle);


        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
            String command = e.getActionCommand();

            if(command=="new") {
                JOptionPane.showMessageDialog(this, "开始游戏");
            }
            else if(command=="undo") {
                JOptionPane.showMessageDialog(this, "悔棋");
            }
            else if(command=="surrender") {
                int r =JOptionPane.showConfirmDialog(this, "确定要认输吗","提示",JOptionPane.OK_CANCEL_OPTION);
                System.out.println(r); //确定为0, 取消为 2
            }
            else if(command=="exit") {
                System.exit(0);
            }
            else if(command=="about") {
                JOptionPane.showMessageDialog(this, "关于...");
            }
        }
}
